// pages/Collection/Collection.js
var http = require('../../utils/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sts: 0,
		prodList: [],
		title: "",
		current: 1,
		size: 10,
		pages: 0,
		tagid: 0
  },

 /**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.setData({
			current: 1,
			pages: 0,
			sts: options.sts,
			title: options.title ? options.title : ""
		});
		if (options.tagid) {
			this.setData({
				tagid: options.tagid
			});
		}

		if (this.data.sts == 0) {
			if (options.tagid == 1) {
				wx.setNavigationBarTitle({
					title: '每日上新'
				})
			} else if (options.tagid == 2) {
				wx.setNavigationBarTitle({
					title: '商城热卖'
				})
			} else if (options.tagid == 3) {
				wx.setNavigationBarTitle({
					title: '更多宝贝'
				})
			}
		} else if (this.data.sts == 1) {
			wx.setNavigationBarTitle({
				title: '新品推荐',
			})
		} else if (this.data.sts == 2) {
			wx.setNavigationBarTitle({
				title: '限时特惠',
			})
		} else if (this.data.sts == 3) {
			wx.setNavigationBarTitle({
				title: '每日疯抢',
			})
		} else if (this.data.sts == 4) {
			wx.setNavigationBarTitle({
				title: '优惠券活动商品',
			})
		} else if (this.data.sts == 5) {
			wx.setNavigationBarTitle({
				title: '我的收藏商品',
			})
		} else {
			wx.setNavigationBarTitle({
				title: this.data.title
			})
		}
		this.loadProdData(options);

	},

	/**
	 * 加载商品数据
	 */
	loadProdData: function(options) {
		let sts = this.data.sts
		if (sts == 0) {
			// 分组标签商品列表
			this.getTagProd();
		} else if (sts == 1) {
			// 新品推荐
			let url = "/prod/lastedProdPage"
			this.getActProd(url)
		} else if (sts == 2) {
			// 限时特惠
			let url = "/prod/discountProdList"
			this.getActProd(url)
		} else if (sts == 3) {
			// 每日疯抢
			let url = "/prod/moreBuyProdList"
			this.getActProd(url)
		} else if (sts == 4) {
			// 优惠券商品列表
			this.getProdByCouponId(options.tagid)
		} else if (sts == 5) {
			// 收藏商品列表
			this.getCollectionProd()
		}
	},

	getActProd: function(url) {
		var ths = this;
		wx.showLoading();
		request({
			url: url,
			method: "GET",
			data: {
				current: ths.data.current,
				size: ths.data.size,
			}

		}).then(res => {
			let list = []
			if (res.current == 1) {
				list = res.records
			} else {
				list = ths.data.prodList
				list = list.concat(res.records)
			}
			ths.setData({
				prodList: list,
				pages: res.pages
			});
			wx.hideLoading();
		})
	},

	/**
	 * 获取我的收藏商品
	 */
	getCollectionProd: function() {
		var ths = this;
		wx.showLoading();
		request({
			url: "/p/user/collection/prods",
			method: "GET",
			header: {
				'Authorization': "7Jr/9m22Ty+r96r/kyX+ejusJzlakoKY6ZqVdAtPBBOFdSG/emDQUuO7h8151+4/"
			},
			data: {
				current: ths.data.current,
				size: ths.data.size,
			}
		}).then(res => {
			let list = []
			if (res.current == 1) {
				list = res.records
			} else {
				list = ths.data.prodList
				list = list.concat(res.records)
			}
			ths.setData({
				prodList: list,
				pages: res.pages
			});
			wx.hideLoading();
		})
	},

	/**
	 * 获取标签列表
	 */
	getTagProd: function(id) {
		var ths = this;
		wx.showLoading();
		request({
			url: "/prod/prodListByTagId",
			method: "GET",
			data: {
				tagId: ths.data.tagid,
				current: ths.data.current,
				size: ths.data.size
			}
		}).then(res => {
			let list = []
			if (res.current == 1) {
				list = res.records
			} else {
				list = ths.data.prodList.concat(res.records)
			}
			ths.setData({
				prodList: list,
				pages: res.pages
			});
			wx.hideLoading();
		})

	},

	/**
	 * 获取优惠券商品列表
	 */
	getProdByCouponId(id) {
		var ths = this;
		wx.showLoading();
		request({
			url: "/coupon/prodListByCouponId",
			method: "GET",
			data: {
				couponId: id,
				current: this.data.current,
				size: this.data.size
			}
		}).then(res => {
			let list = []
			if (res.current == 1) {
				list = res.records
			} else {
				list = ths.data.prodList.concat(res.records)
			}
			ths.setData({
				prodList: list,
				pages: res.pages
			});
			wx.hideLoading();
		})
	},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})